<?php
/**
 * model
 * 模型
 * @author Administrator
 * @version
 */
class Model_Tables_Mm extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = '';
    public function add( $params,$minfo )
    {
    	$tbstr = "";
    	$vstr = "";
    	$paramv = array();
    	foreach($params as $k=>$v){
    		if($k!="id"){
    			$tbstr.=$k.",";
    			$vstr.=":".$k.",";
    			if(is_array($v))
    				$paramv[$k] = implode(",",$v);
    			else
    				$paramv[$k] = $v;
    		}
    	}
    	$tbstr = trim($tbstr,",");
    	$vstr = trim($vstr,",");
        $db = $this->getAdapter();
        return $db->query("insert into " . $minfo['tablename'] . "($tbstr) values($vstr)",$paramv);
    }
    
    public function modify( $params,$minfo )
    {
    	$vstr = "";
    	$paramv = array();
    	foreach($params as $k=>$v){
    		if($k!="id"){
    			$vstr.= $k."=:".$k.",";
    		}
	    	if(is_array($v))
	    		$paramv[$k] = implode(",",$v);
	    	else
	    		$paramv[$k] = $v;
    	}
    	$tbstr = trim($tbstr,",");
    	$vstr = trim($vstr,",");
        $db = $this->getAdapter();
        //echo "update " . $minfo['tablename'] . " set $vstr where id=:id";exit;
        return $db->query("update " . $minfo['tablename'] . " set $vstr where id=:id",$paramv);
    	
    }
    public function modifyAjax( $modelid, $field, $value,$oldvalue )
    {
    	
    }
  	public function modifyAjaxRevers( $field ,$pri)
    {
    	
    }
    public function del($minfo,$id)
    {
    	$db = $this->getAdapter();
    	return $db->query("delete from `" . $minfo['tablename'] . "` where id=:id",array("id"=>$id));
    }
    public function getInfo($id,$minfo)
    {
    	$db = $this->getAdapter();
    	$row = $db->fetchRow("select * from " . $minfo['tablename']. " where id=:id",array("id"=>$id));
    	return $row;
    }
    public function getCount($minfo,$infos,$s="")
    {
    	$db = $this->getAdapter();
    	if($s!="")
    	{
    		$where = $db->quoteInto(" where id = ?", $s);
    		foreach($infos as $item){
    			if($item["isfulltext"]){
    				$where.= $db->quoteInto(" or " . $item["field"] . " like ?", "%$s%");
    			}
    		}
    	}
    	//echo "select count(*) from " . $minfo['tablename'] . " " . $where;exit;
    	$count = $db->fetchOne("select count(*) from " . $minfo['tablename'] . " " . $where);
    	return $count;
    }
    public function getList($minfo, $infos, $page, $numPerPage = 10,$s="")
    {
    	$db = $this->getAdapter();
    	if($s!="")
    	{
    		$where = $db->quoteInto(" where id = ?", $s);
    		foreach($infos as $item){
    			if($item["isfulltext"]){
    				$where.= $db->quoteInto(" or " . $item["field"] . " like ?", "%$s%");
    			}
    		}
    	}
    	//echo "select * from " . $minfo['tablename'] . " " . $where;exit;
    	$result = $db->fetchAll("select * from " . $minfo['tablename'] . " " . $where);
    	return $result;
    }
}
 
 